using CoreManager.Elasticsearch.Models;

namespace CoreManager.Elasticsearch.Services;

/// <summary>
/// Elasticsearch服务接口
/// </summary>
public interface IElasticsearchService
{
    /// <summary>
    /// 创建索引
    /// </summary>
    /// <param name="indexName">索引名称</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns></returns>
    Task<bool> CreateIndexAsync(string indexName, CancellationToken cancellationToken = default);
    
    /// <summary>
    /// 删除索引
    /// </summary>
    /// <param name="indexName">索引名称</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns></returns>
    Task<bool> DeleteIndexAsync(string indexName, CancellationToken cancellationToken = default);
    
    /// <summary>
    /// 检查索引是否存在
    /// </summary>
    /// <param name="indexName">索引名称</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns></returns>
    Task<bool> IndexExistsAsync(string indexName, CancellationToken cancellationToken = default);
    
    /// <summary>
    /// 索引文档
    /// </summary>
    /// <typeparam name="T">文档类型</typeparam>
    /// <param name="indexName">索引名称</param>
    /// <param name="document">文档</param>
    /// <param name="documentId">文档ID（可选）</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns></returns>
    Task<IndexResult> IndexDocumentAsync<T>(string indexName, T document, string? documentId = null, CancellationToken cancellationToken = default) where T : class;
    
    /// <summary>
    /// 批量索引文档
    /// </summary>
    /// <typeparam name="T">文档类型</typeparam>
    /// <param name="indexName">索引名称</param>
    /// <param name="documents">文档列表</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns></returns>
    Task<List<IndexResult>> BulkIndexDocumentsAsync<T>(string indexName, IEnumerable<T> documents, CancellationToken cancellationToken = default) where T : class;
    
    /// <summary>
    /// 获取文档
    /// </summary>
    /// <typeparam name="T">文档类型</typeparam>
    /// <param name="indexName">索引名称</param>
    /// <param name="documentId">文档ID</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns></returns>
    Task<T?> GetDocumentAsync<T>(string indexName, string documentId, CancellationToken cancellationToken = default) where T : class;
    
    /// <summary>
    /// 删除文档
    /// </summary>
    /// <param name="indexName">索引名称</param>
    /// <param name="documentId">文档ID</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns></returns>
    Task<bool> DeleteDocumentAsync(string indexName, string documentId, CancellationToken cancellationToken = default);
    
    /// <summary>
    /// 搜索文档
    /// </summary>
    /// <typeparam name="T">文档类型</typeparam>
    /// <param name="indexName">索引名称</param>
    /// <param name="request">搜索请求</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns></returns>
    Task<Models.SearchResponse<T>> SearchAsync<T>(string indexName, Models.SearchRequest request, CancellationToken cancellationToken = default) where T : class;
    
    /// <summary>
    /// 测试连接
    /// </summary>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns></returns>
    Task<bool> PingAsync(CancellationToken cancellationToken = default);
    
    /// <summary>
    /// 获取集群健康状态
    /// </summary>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns></returns>
    Task<object?> GetClusterHealthAsync(CancellationToken cancellationToken = default);
}

